/**
 * Helper
 *
 * Released under Apache License, Version 2.0.
 */
 
package bsp.helper;

import java.util.*;
import java.text.*;

/**
 * Utility class.
 */
public class Helper
{
	protected Helper()
	{
		/* empty by design */
	}
	
	/**
	 * Sleep.
	 * 
	 * @param time Time to sleep for, in milliseconds.
	 * @return Result of execution.
	 */
	public static boolean sleep(int time)
	{
		boolean retVal = false;
		
		try {
			Thread.sleep(time);
			retVal = true;
		} catch ( InterruptedException ex ) {
			ex.printStackTrace();
		}
		
		return retVal;
	}
	
	/**
	 * Path a class dynamically.
	 * 
	 * @param className
	 * @return Object instance
	 */
	public static Object getInstanceFromClass(String className)
	{
		Object obj = null;
		
		try {
			Class<?> c = Class.forName(className);
			obj = c.newInstance();
		} catch ( ClassCastException ex ) {
			ex.printStackTrace();
		} catch ( ClassNotFoundException ex ) {
			ex.printStackTrace();
		} catch ( IllegalAccessException ex ) {
			ex.printStackTrace();
		} catch ( InstantiationException ex ) {
			ex.printStackTrace();
		}
		
		return obj;
	}
	
	/**
	 * Convert string into a date object using format specified.
	 * 
	 * @param dateStr String to be converted.
	 * @param format Format for conversion
	 * @return Date
	 */
	public static Date getDateFromString(String dateStr, String format)
	{
		DateFormat formatter;
		Date date = null;
		
		try {
			formatter = new SimpleDateFormat(format);
			date = (Date)formatter.parse(dateStr);
		} catch ( ParseException ex ) {
			ex.printStackTrace();
		}
		
		return date;
	}
	
	/**
	 * Convert string into a calendar object using format specified.
	 * 
	 * @param dateStr String to be converted.
	 * @param format Format for conversion
	 * @return Calendar
	 */
	public static Calendar getCalendarFromString(String dateStr, String format)
	{
		DateFormat formatter;
		Calendar cal = null;
		
		try {
			formatter = new SimpleDateFormat(format);
			Date date = formatter.parse(dateStr);
			cal = Calendar.getInstance();
			cal.setTime(date);
		} catch ( ParseException ex ) {
			ex.printStackTrace();
		}
		
		return cal;
	}
	
	/**
	 * 
	 * @param time Date
	 * @param format String
	 * @return Date
	 */
	public static Date convertDate(Date time, String format)
	{
		DateFormat tmpDateFormat = new SimpleDateFormat(format);
		Date newDate = null;
		
		try {
		     newDate = tmpDateFormat.parse(time.toString());
		} catch (ParseException e) {
			//System.out.println("Invalid Date Parser Exception ");
		     e.printStackTrace();
		}
		
		return newDate;
	}
	
	/**
	 * 
	 * @param time Calendar
	 * @param format String
	 * @return String
	 */
	public static String convertDateToStringBipin(Calendar time, String format)
	{
		String retVal = null;
		StringBuffer str = new StringBuffer(250);
		String[] months = {
			"Jan", "Feb", "Mar", "Apr", "May", "Jun",
			"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
		};
		
		int year = time.get(Calendar.YEAR);
		int month = time.get(Calendar.MONTH);
		int day = time.get(Calendar.DAY_OF_MONTH);
		
		String sday;
		if ( day < 10 ) {
			sday = "0" + day;
		} else {
			sday = new Integer(day).toString();
		}
		
		if ( true == "dd-MMM-yyyy".equals(format) ) {
			str.append(sday).append("-").append(months[month]).append("-").append(year);
		} else if ( true == "dd-MMM-yyyy".equals(format) ) {
			str.append(year).append("-").append(months[month]).append("-").append(sday);
		}
		
		if ( str.length() > 0 ) {
			retVal = str.toString();
		}
		
		return retVal;
	}
	
	/**
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static String convertDateToString(Date date, String format)
	{
		String str = new SimpleDateFormat(format).format(date);
		return str;
	}
	
	/**
	 * 
	 * @param time
	 * @param format
	 * @return
	 */
	public static String convertCalendarToString(Calendar time, String format)
	{
		Date date = time.getTime();
		String str = new SimpleDateFormat(format).format(date);
		return str;
	}
	
	/**
	 * 
	 * @param lhm
	 * @param key
	 * @param defValue
	 * @return
	 */
	public static boolean getBooleanFieldDefault(LinkedHashMap<String, Boolean> lhm, String key, boolean defValue)
	{
		boolean retVal = defValue;
		
		if ( lhm.containsKey(key) ) {
			retVal = lhm.get(key);
		}
		
		return retVal;
	}
	
	/**
	 * 
	 * @param lhm
	 * @param key
	 * @return
	 */
	public static boolean getBooleanField(LinkedHashMap<String, Boolean> lhm, String key)
	{
		return getBooleanFieldDefault(lhm, key, false);
	}
	
	/**
	 * 
	 * @param lhm
	 * @param key
	 * @param defValue
	 * @return
	 */
	public static String getStringFieldDefault(LinkedHashMap<String, String> lhm, String key, String defValue)
	{
		String retVal = defValue;
		
		if ( lhm.containsKey(key) ) {
			retVal = lhm.get(key);
		}
		
		return retVal;
	}
	
	/**
	 * 
	 * @param lhm
	 * @param key
	 * @return
	 */
	public static String getStringField(LinkedHashMap<String, String> lhm, String key)
	{
		return getStringFieldDefault(lhm, key, null);
	}
	
	/**
	 * 
	 * @param lhm
	 * @param key
	 * @param defValue
	 * @return
	 */
	public static int getIntegerFieldDefault(LinkedHashMap<String, Integer> lhm, String key, int defValue)
	{
		int retVal = defValue;
		
		if ( lhm.containsKey(key) ) {
			retVal = lhm.get(key);
		}
		
		return retVal;
	}
	
	/**
	 * 
	 * @param lhm
	 * @param key
	 * @return
	 */
	public static int getIntegerField(LinkedHashMap<String, Integer> lhm, String key)
	{
		return getIntegerFieldDefault(lhm, key, 0);
	}
	
	public static boolean isStringADouble(String value)
	{
		boolean retVal = false;
		
		try {
			Double.parseDouble(value);
			retVal = true;
		} catch ( NumberFormatException ex ) {
			retVal = false;
		}
		
		return retVal;
	}
	
	public static boolean isStringAnInteger(String value)
	{
		boolean retVal = false;
		
		try {
			Integer.parseInt(value);
			retVal = true;
		} catch ( NumberFormatException ex ) {
			retVal = false;
		}
		
		return retVal;
	}
	
	public static long getCalendarTimeMilliSeconds()
	{
		return Calendar.getInstance().getTimeInMillis();
	}
	
	public static long getSystemTimeMilliSeconds()
	{
		return System.currentTimeMillis();
	}
}

/* end */
